package com.example.example;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this, "1bfc00caac31c7809a1a6f25d8c1396f");

        Button btn_add = findViewById(R.id.btn_add);
        Button btn_update = findViewById(R.id.btn_update);
        Button btn_query = findViewById(R.id.btn_query);
        Button btn_delete = findViewById(R.id.btn_delete);

        List<Category> list = new ArrayList<>();

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Category category = new Category();
                category.setName("football");
                category.setDesc("足球");
                category.setSequence(list.size() + 1);
                category.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e == null) {
                            Log.d("BMOB", "添加成功");
                            category.setmObjectId(s);
                            Toast.makeText(MainActivity.this,
                                    category.getmObjectId() + " 添加成功", Toast.LENGTH_LONG).show();
                            list.add(category);
                        } else {
                            Log.e("BMOB", "添加失败");
                            Toast.makeText(MainActivity.this,
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });

        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Category category = new Category();
                category.setName("basketball");
                category.setDesc("篮球");
                if(list.size() != 0){
                    category.update(list.get(list.size() - 1).getmObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null){
                                Log.d("BMOB", "更新成功");
                                Toast.makeText(MainActivity.this,
                                        list.get(list.size() - 1).getmObjectId() + " 更新成功", Toast.LENGTH_LONG).show();
                            } else {
                                Log.e("BMOB", "更新失败");
                                Toast.makeText(MainActivity.this,
                                        e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }else{
                    Log.e("BMOB", "没有可更新数据");
                    Toast.makeText(MainActivity.this,
                            "没有可更新数据", Toast.LENGTH_LONG).show();
                }
            }
        });

        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuery<Category> bmobQuery = new BmobQuery<>();
                if(list.size() != 0){
                    bmobQuery.getObject(list.get(list.size() - 1).getmObjectId(), new QueryListener<Category>() {
                        @Override
                        public void done(Category category, BmobException e) {
                            if(e == null){
                                Log.d("BMOB", "查找成功");
                                Toast.makeText(MainActivity.this, "查找成功", Toast.LENGTH_LONG).show();
                            }else{
                                Log.e("BMOB", "查找失败");
                                Toast.makeText(MainActivity.this,
                                        e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                } else {
                    Log.e("BMOB", "没有可查找数据");
                    Toast.makeText(MainActivity.this,
                            "没有可查找数据", Toast.LENGTH_LONG).show();
                }
            }
        });

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Category category = new Category();
                if(list.size() != 0){
                    category.delete(list.get(0).getmObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e == null){
                                Toast.makeText(MainActivity.this,
                                        list.get(0).getmObjectId() + " 删除成功", Toast.LENGTH_LONG).show();
                                if(list.size() >= 1){
                                    list.remove(0);
                                }
                                Log.d("BMOB", "删除成功");
                            }else{
                                Log.e("BMOB", e.toString());
                                Toast.makeText(MainActivity.this,
                                        e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }else {
                    Log.e("BMOB", "没有可删除数据");
                    Toast.makeText(MainActivity.this, "没有可删除数据", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
